"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : standby_statement_history
Case Name   : guc参数:track_stmt_standby_chain_size,修改方式为gs_guc reload,
             参数1,3修改为边界右值,参数2,4为默认值,
Create At   : 2022/10/17
Owner       : @li-xin12345
Description :
    1.查看当前track_stmt_standby_chain_size参数
    2.将参数1,3修改为边界右值,参数2,4为默认值
    3.查看当前track_stmt_standby_chain_size参数
    4.将参数1,3修改为边界右值,参数2修改为默认值,参数4为正常值
    5.查看当前track_stmt_standby_chain_size参数
    6.恢复track_stmt_standby_chain_size参数默认值
Expect      :
    1.成功
    2.执行成功但修改未生效
    3.参数值仍为原值
    4.修改成功
    5.成功
    6.恢复成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == primary_sh.get_node_num(),
                 'Single node, and subsequent codes are not executed.')
class StandbyStatementHistory(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.standby_node = Node('Standby1DbUser')
        self.standby_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.common = Common('Standby1DbUser')
        self.chain_size = 'track_stmt_standby_chain_size'
        self.postgresql_path = os.path.join(macro.DB_INSTANCE_PATH,
                                            macro.DB_PG_CONFIG_NAME)

        text = "-----step1:查看当前track_stmt_standby_chain_size参数;expect:成功-----"
        self.logger.info(text)
        self.chain_size_default = self.common.show_param(self.chain_size)

    def test_standby_statement_history(self):
        text = "-----step2:将参数1,3修改为边界右值,参数2,4为默认值;" \
               "expect:执行成功但修改未生效-----"
        self.logger.info(text)
        result = self.standby_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'{self.chain_size}=\'1024, 1024, 1024, 512\'')
        self.assertTrue(result, '执行失败' + text)
        shell_cmd = f"grep {self.chain_size} {self.postgresql_path}"
        shell_msg = self.common.get_sh_result(self.standby_node, shell_cmd)
        self.assertIn('1024, 1024, 1024, 512', shell_msg, '执行失败' + text)

        text = "-----step3:查看当前track_stmt_standby_chain_size参数;" \
               "expect:参数值仍为原值-----"
        self.logger.info(text)
        show_msg = self.common.show_param(self.chain_size)
        self.assertIn(self.chain_size_default, show_msg, '执行失败' + text)

        text = "-----step4:将参数1,3修改为边界右值,参数2修改为默认值," \
               "参数4为正常值;expect:修改成功-----"
        self.logger.info(text)
        result = self.standby_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'{self.chain_size}=\'1024, 1024, 1024, 2048\'')
        self.assertTrue(result, '执行失败' + text)

        text = "-----step5:查看当前track_stmt_standby_chain_size参数;expect:成功-----"
        self.logger.info(text)
        show_msg = self.common.show_param(self.chain_size)
        self.assertIn('1024, 1024, 1024, 2048', show_msg, '执行失败' + text)

    def tearDown(self):
        text = "-----step6:恢复track_stmt_standby_chain_size参数默认值;" \
               "expect:恢复成功-----"
        self.logger.info(text)
        result = self.standby_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'{self.chain_size}=\'{self.chain_size_default}\'')
        show_msg = self.common.show_param(self.chain_size)
        self.assertTrue(result, '执行失败' + text)
        self.assertIn(self.chain_size_default, show_msg, '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
